
/**
 * AppleIIGo
 * Keyboard class
 * (C) 2008 by Marc S. Ressl (mressl@umich.edu)
 * Released under the GPL
 */
 
#include "Motherboard.h"

#ifndef KEYBOARD_H
#define KEYBOARD_H

typedef enum {
	KEYBOARDLANGUAGE_DEFAULT,
	KEYBOARDLANGUAGE_UK,
	KEYBOARDLANGUAGE_FRENCH,
	KEYBOARDLANGUAGE_DANISH,
	KEYBOARDLANGUAGE_SPANISH,
	KEYBOARDLANGUAGE_ITALIAN,
	KEYBOARDLANGUAGE_GERMAN,
	KEYBOARDLANGUAGE_SWEDISH,
	KEYBOARDLANGUAGE_END,
};

#define UNICODE_INVEXCLAMATION		0xa1
#define UNICODE_POUND				0xa3
#define UNICODE_SECTION				0xa7
#define UNICODE_DIAERESIS			0xa8
#define UNICODE_DEGREE				0xb0
#define UNICODE_INVQUESTION			0xbf
#define UNICODE_CAPITALADIAERESIS	0xc4
#define UNICODE_CAPITALARING		0xc5
#define UNICODE_CAPITALAE			0xc6
#define UNICODE_CAPITALNTILDE		0xd1
#define UNICODE_CAPITALODIAERESIS	0xd6
#define UNICODE_CAPITALOSTROKE		0xd8
#define UNICODE_CAPITALUDIAERESIS	0xdc
#define UNICODE_SHARPS				0xdf
#define UNICODE_AGRAVE				0xe0
#define UNICODE_ADIAERESIS			0xe4
#define UNICODE_ARING				0xe5
#define UNICODE_AE					0xe6
#define UNICODE_CCEDILLA			0xe7
#define UNICODE_EGRAVE				0xe8
#define UNICODE_EACUTE				0xe9
#define UNICODE_IGRAVE				0xec
#define UNICODE_NTILDE				0xf1
#define UNICODE_OGRAVE				0xf2
#define UNICODE_ODIAERESIS			0xf6
#define UNICODE_OSTROKE				0xf8
#define UNICODE_UGRAVE				0xf9
#define UNICODE_UDIAERESIS			0xfc

typedef enum {
	KEYMODIFIER_SHIFT,
	KEYMODIFIER_CONTROL,
	KEYMODIFIER_CAPSLOCK,
	KEYMODIFIER_KEYPAD,
	KEYMODIFIER_OPTION,
	KEYMODIFIER_COMMAND,
	KEYMODIFIER_END,
};

class Keyboard : public Device {
public:
	Keyboard(class Motherboard * motherboard, bool isLowercase);
	
	void onDevicesConnected();
	void onResetAsserted(bool isRestart);
	
	void setKeyLatch(UINT32 unicode);
	UINT8 getKeyLatch();
	void clearKeyLatch();
	bool isKeyLatchClear();
	void pushKey(UINT32 unicode);
	
	void setAnyKeyDown(bool value);
	bool isAnyKeyDown();
	
	void pushKeyModifier(UINT32 index);
	void pullKeyModifier(UINT32 index);
	bool isKeyModifierDown(UINT32 index);
	
	void paste(char * buffer);
	
	void setLanguage(UINT32 value);
	UINT32 getLanguage();
	
	void toggleIIc40Switch();
	bool isIIc40Switch();
	
private:
	class Paddles * paddles;
	class ADB * adb;
	
	bool isLowercase;
	UINT8 keyLatch;
	UINT32 keyModifierCount[KEYMODIFIER_END];
	bool isAnyKeyDownValue;
	UINT32 languageIndex;
	
	char * pasteBuffer;
	UINT32 pasteBufferCharIndex;
	UINT32 pasteBufferCharNum;

	bool isIIc40SwitchValue;
	
	void initKeyLatch(bool isLowercase);
	void initKeyModifier();
	void initPasteBuffer();
	void clearPasteBuffer();
	UINT8 getPasteBufferByte();
	void pasteNextKey();
	void updateAdbKeyModifier();
	void initIIc40Switch();
};

#endif
